{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: NumericalConcept Extraction"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: NumericalConcept Extraction\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, NumericalConcept\n",
        "\n",
        "\n",
        "# Create a Document object from text\n",
        "doc = Document(\n",
        "    raw_text=\"The latest smartphone model costs $899.99 and will be available next week.\"\n",
        ")\n",
        "\n",
        "# Define a NumericalConcept to extract the price\n",
        "price_concept = NumericalConcept(\n",
        "    name=\"Product price\",\n",
        "    description=\"The price of the product\",\n",
        "    numeric_type=\"float\",  # We expect a decimal price\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([price_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document\n",
        "price_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the extracted value\n",
        "print(price_concept.extracted_items[0].value)  # Output: 899.99\n",
        "# Or access the extracted value from the document object\n",
        "print(doc.concepts[0].extracted_items[0].value)  # Output: 899.99\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}